﻿using Microsoft.Practices.Prism.Events;
using compshop.Infrastructure;

namespace compshop.Modules.PositionList
{
    public class EventsManager
    {
        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;

        public bool IsActive = false;
        public Presenter.PositionListPresenter positionListPresenter { get; set; }

        public EventsManager(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }
        
        public IEventAggregator EventAggregator
        {
            get { return eventAggregator; }
        }

        private void ActivityProcessor(ModuleActivityState state)
        {
            if (state.ActivateModule == RegionNames.PositionListContent)
            {
                if (!IsActive)
                {
                    // activation code place here
                    IsActive = true;
                }
            }
            else
            {
                if (state.DeactivateAll)
                {
                    if (IsActive)
                    {
                        // deactivation code place here
                        IsActive = false;
                    }
                }
            }
        }

        public void Subscribe()
        {
            ModuleActivityEvent moduleActivityEvent = eventAggregator.GetEvent<ModuleActivityEvent>();
            moduleActivityEvent.Subscribe(this.ActivityProcessor); 
          
            DepartmentSelectedEvent selectedEvent = eventAggregator.GetEvent<DepartmentSelectedEvent>();
            subscriptionToken = selectedEvent.Subscribe(positionListPresenter.SetSelectedPosition, ThreadOption.UIThread, false, ActivityFilter);
        }

        public bool ActivityFilter(int a)
        {
            return IsActive;
        }
    }
}
